<template>
  <div class="app-container">
    <el-form ref="queryForm" :model="queryParams" :inline="true" label-width="68px">
      <el-form-item label="家庭码" prop="familyCode">
        <el-input
          v-model="queryParams.familyCode"
          placeholder="请输入家庭码"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="孩子昵称" prop="nickname">
        <el-input
          v-model="queryParams.nickname"
          placeholder="请输入孩子昵称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="孩子性别" prop="sex">
        <el-select v-model="queryParams.sex" clearable placeholder="请选择性别" size="small">
          <el-option
            v-for="item in sexOptions"
            :key="item.dictValue"
            :label="item.dictLabel"
            :value="item.dictValue.toString()"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="孩子类型" prop="childType">
        <el-select v-model="queryParams.childType" clearable placeholder="请选择类型" size="small">
          <el-option
            v-for="item in childtypeOptions"
            :key="item.dictValue"
            :label="item.dictLabel"
            :value="item.dictValue.toString()"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="生日" prop="birthday">
        <el-date-picker
          v-model="queryParams.birthday"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          size="small"
        ></el-date-picker>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          v-permisaction="['child:child:add']"
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          v-permisaction="['child:child:edit']"
          type="success"
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          v-permisaction="['child:child:remove']"
          type="danger"
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
        >删除</el-button>
      </el-col>
    </el-row>

    <el-table v-loading="loading" :data="childList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />

      <el-table-column label="头像" align="center" prop="avatar" :show-overflow-tooltip="true">
        <template slot-scope="scope">
          <el-avatar :size="50" :src="scope.row.avatar"></el-avatar>
        </template>
      </el-table-column>

      <el-table-column label="孩子昵称" align="center" prop="nickname" :show-overflow-tooltip="true" />
      <el-table-column label="性别" align="center" prop="sex" :show-overflow-tooltip="true" />

      <el-table-column
        label="生日"
        align="center"
        prop="birthday"
        :show-overflow-tooltip="true"
        width="140"
      />
      <el-table-column label="月龄" align="center" prop="ageNum" :show-overflow-tooltip="true" />

      <el-table-column label="身高" align="center" prop="height" :show-overflow-tooltip="true" />
      <el-table-column label="体重" align="center" prop="weight" :show-overflow-tooltip="true" />

      <el-table-column label="父亲身高" align="center" prop="heightDad" :show-overflow-tooltip="true" />

      <el-table-column label="母亲身高" align="center" prop="heightMum" :show-overflow-tooltip="true" />
      <el-table-column
        label="遗传身高"
        align="center"
        prop="heightInherit"
        :show-overflow-tooltip="true"
      />

      <el-table-column
        label="家庭码"
        align="center"
        prop="familyCode"
        :show-overflow-tooltip="true"
        width="160"
      />
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
        width="220"
        fixed="right"
      >
        <template slot-scope="scope">
          <el-button
            v-permisaction="['child:child:edit']"
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
          >修改</el-button>
          <el-button
            v-permisaction="['child:child:remove']"
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
          >删除</el-button>

          <el-button
            v-permisaction="['child:child:report']"
            size="mini"
            type="text"
            icon="el-icon-data-analysis"
            @click="handleReport(scope.row)"
          >测评分析</el-button>
          <el-button
            v-permisaction="['child:child:height']"
            size="mini"
            type="text"
            icon="el-icon-data-line"
            @click="handleHeight(scope.row)"
          >身高记录</el-button>
          <el-button
            v-permisaction="['child:child:weight']"
            size="mini"
            type="text"
            icon="el-icon-data-line"
            @click="handleWeight(scope.row)"
          >体重记录</el-button>
          <el-button
            v-permisaction="['child:child:clocked']"
            size="mini"
            type="text"
            icon="el-icon-tickets"
             @click="handleClockLog(scope.row)"
          >打卡记录</el-button>

          <el-button
            v-permisaction="['child:child:health']"
            size="mini"
            type="text"
            icon="el-icon-tickets"
            @click="handleHealth(scope.row)"
          >儿保记录</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageIndex"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改参数配置对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" style="align-items:flex-start">
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="孩子头像" prop="avatar">
          <el-avatar :size="80" :src="form.avatar"></el-avatar>
        </el-form-item>
        <el-form-item label="孩子昵称" prop="nickname">
          <el-input v-model="form.nickname" placeholder="孩子昵称" style="width:200px" />
        </el-form-item>

        <el-form-item label="性别" prop="sex">
          <el-radio-group v-model="form.sex">
            <el-radio
              v-for="(item,index) in sexOptions "
              :key="index"
              :label="Number(item.dictValue)"
            >{{item.dictLabel}}</el-radio>
          </el-radio-group>
        </el-form-item>

        <el-form-item label="生日" prop="birthday">
          <el-date-picker
            v-model="form.birthday"
            type="date"
            placeholder="生日"
            format="yyyy 年 MM 月 dd 日"
            value-format="yyyy-MM-dd"
            style="width:200px"
          ></el-date-picker>
        </el-form-item>

        <el-form-item label="身高" prop="height">
          <el-input-number v-model="form.height" :precision="2" :step="0.1" :max="220" :min="0"></el-input-number>
          <i>CM</i>
        </el-form-item>
        <el-form-item label="体重" prop="weight">
          <el-input-number v-model="form.weight" :precision="2" :step="0.1" :max="200" :min="0"></el-input-number>
          <i>KG</i>
        </el-form-item>

        <el-form-item label="孩子类型" prop="userType">
          <el-radio-group v-model="form.childType">
            <el-radio
              v-for="(item,index) in childtypeOptions "
              :key="index"
              :label="Number(item.dictValue)"
            >{{item.dictLabel}}</el-radio>
          </el-radio-group>
        </el-form-item>

        <el-form-item label="父亲身高" prop="heightDad">
          <el-input-number
            v-model="form.heightDad"
            :precision="2"
            :step="0.1"
            :max="220"
            :min="0"
            @change="handleInheritHeight"
            @blur="handleInheritHeight"
          ></el-input-number>
          <i>CM</i>
        </el-form-item>

        <el-form-item label="母亲身高" prop="heightMum">
          <el-input-number
            v-model="form.heightMum"
            :precision="2"
            :step="0.1"
            :max="220"
            :min="0"
            @change="handleInheritHeight"
            @blur="handleInheritHeight"
          ></el-input-number>
          <i>CM</i>
        </el-form-item>

        <el-form-item label="遗传身高" prop="heightInherit">
          <el-input-number
            v-model="form.heightInherit"
            :precision="2"
            :step="0.1"
            :max="220"
            :min="0"
            disabled="true"
          ></el-input-number>
          <i>CM</i>
        </el-form-item>

        <el-form-item label="家庭码" prop="familyCode">
          <el-input v-model="form.familyCode" placeholder="家庭code" style="width:200px" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <el-dialog :title="title_height" :visible.sync="open_height" width="800px">
      <ChildHeight :childId="childRow.childId" :childData="childRow" />
    </el-dialog>

    <el-dialog :title="title_weight" :visible.sync="open_weight" width="800px">
      <ChildWeight :childId="childRow.childId" :childData="childRow" />
    </el-dialog>

    <el-dialog :title="title_report" :visible.sync="open_report" width="800px" style="align-items:flex-start">
      <ChildReport :childId="childRow.childId" :childData="childRow" />
    </el-dialog>

    <el-dialog :title="title_health" :visible.sync="open_health" width="800px">
      <ChildHealth :childId="childRow.childId" :childData="childRow" />
    </el-dialog>

    <el-dialog :title="title_clockLog" :visible.sync="open_clockLog" width="800px">
      <ChildClocklog :childId="childRow.childId" :childData="childRow" />
    </el-dialog>

  </div>
</template>

<script>
import {
  listchild,
  getchild,
  delchild,
  addchild,
  updatechild,
  getInheritHeight,
} from "@/api/child";

import ChildHeight from "@/components/MychildHeight";
import ChildWeight from "@/components/MychildWeight";
import ChildReport from "@/components/MychildReport";
import ChildHealth from "@/components/MychildHealth";
import ChildClocklog from "@/components/MychildClocklog";

export default {
  name: "child",
  components: {
    ChildHeight,
    ChildWeight,
    ChildReport,
    ChildHealth,
    ChildClocklog,
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 总条数
      total: 0,
      // 孩子表格数据
      childList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 类型数据字典
      typeOptions: [],
      // 日期范围
      dateRange: [],
      // 查询参数
      queryParams: {
        pageIndex: 1,
        pageSize: 10,
        birthday: undefined,
        familyCode: undefined,
        nickname: undefined,
        sex: undefined,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        birthday: [
          { required: true, message: "生日不能为空", trigger: "blur" },
        ],
        familyCode: [],
        nickname: [
          { required: true, message: "孩子昵称不能为空", trigger: "blur" },
        ],
        sex: [
          {
            required: true,
            message:
              "用户的性别，值为0时是男性，值为1时是女性，值为2时是未知不能为空",
            trigger: "blur",
          },
        ],
      },
      //列表
      generaluserList: [],
      //性别选项
      sexOptions: [],
      //孩子类型
      childtypeOptions: [],
      //是否
      boolOptions: [],
      // 身高记录弹出层标题
      title_height: "",
      // 是否显示身高记录弹出层
      open_height: false,
      // 体重记录弹出层标题
      title_weight: "",
      // 是否显示体重记录弹出层
      open_weight: false,
      // 测评报告弹出层标题
      title_report: "",
      // 是否显示测评报告弹出层
      open_report: false,
      // 儿保记录弹出层标题
      title_health: "",
      // 是否显示儿保记录弹出层
      open_health: false,
      // 打卡记录弹出层标题
      title_clockLog: "",
      // 是否显示打卡记录弹出层
      open_clockLog: false,
      //当前选中的孩子数据
      childRow: {},
    };
  },
  created() {
    this.getList();
    this.sexOptions = this.$store.getters.parameter.sys_user_sex;
    this.relationshipOptions = this.$store.getters.parameter.sys_family_relationship;
    this.childtypeOptions = this.$store.getters.parameter.general_user_child_type;
    this.boolOptions = this.$store.getters.parameter.sys_bool;
  },
  methods: {
    /** 查询参数列表 */
    getList() {
      this.loading = true;
      listchild(this.addDateRange(this.queryParams, this.dateRange)).then(
        (response) => {
          this.childList = response.data.list;
          this.total = response.data.count;
          this.loading = false;
        }
      );
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        ageNum: undefined,
        avatar: undefined,
        birthday: undefined,
        childId: undefined,
        childType: 1,
        familyCode: undefined,
        height: 0,
        heightDad: 0,
        heightInherit: 0,
        heightMum: 0,
        nickname: undefined,
        sex: 1,
        weight: 0,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageIndex = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加孩子";
    },

    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.childId);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const childId = row.childId || this.ids;
      getchild(childId).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = "修改孩子";
      });
    },

    //计算遗传身高
    handleInheritHeight() {
      if (this.form.heightDad > 0 && this.form.heightMum > 0) {
        getInheritHeight(this.form).then((response) => {
          this.form.heightInherit = response.data;
        });
      }
    },
    /** 提交按钮 */
    submitForm: function () {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.childId !== undefined) {
            updatechild(this.form).then((response) => {
              if (response.code === 200) {
                this.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              } else {
                this.msgError(response.msg);
              }
            });
          } else {
            addchild(this.form).then((response) => {
              if (response.code === 200) {
                this.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              } else {
                this.msgError(response.msg);
              }
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const Ids = row.childId || this.ids;
      this.$confirm('是否确认删除编号为"' + Ids + '"的数据项?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(function () {
          return delchild(Ids);
        })
        .then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
        .catch(function () {});
    },

    //查看孩子身高记录按钮操作
    handleHeight(row) {
      this.open_height = true;
      this.title_height = row.nickname + "的身高记录";
      this.childRow = row;
    },
    //查看孩子体重记录按钮操作
    handleWeight(row) {
      this.open_weight = true;
      this.title_weight = row.nickname + "的体重记录";
      this.childRow = row;
    },
    //查看孩子测评报告按钮操作
    handleReport(row) {
      this.open_report = true;
      this.title_report = row.nickname + "的测评报告";
      this.childRow = row;
    },
    //查看儿保数据按钮操作
    handleHealth(row) {
      this.open_health = true;
      this.title_health = row.nickname + "的儿保记录";
      this.childRow = row;
    },
    //查看打卡数据按钮操作
    handleClockLog(row) {
      this.open_clockLog = true;
      this.title_clockLog = row.nickname + "的打卡记录";
      this.childRow = row;
    },
  },
};
</script>